package com.liu.usky.rlzp.uskyrlzp.controller;


import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.liu.usky.rlzp.uskyrlzp.controller.base.BaseController;
import com.liu.usky.rlzp.uskyrlzp.entity.Template;
import com.liu.usky.rlzp.uskyrlzp.entity.User;
import com.liu.usky.rlzp.uskyrlzp.service.ITemplateService;
import com.liu.usky.rlzp.uskyrlzp.util.DateUtils;
import com.liu.usky.rlzp.uskyrlzp.util.R;

/**
 * <p>
 * 模板 前端控制器
 * </p>
 *
 * @author lcjzl
 * @since 2020-01-10
 */
@RestController
@RequestMapping("/uskyrlzp/template")
public class TemplateController extends BaseController{
	

	@Autowired
	private ITemplateService iTemplateService;
	
	@PostMapping(value="saveTemplate",produces="application/json")
	public String saveTemplate(@RequestBody Template template) throws JsonProcessingException {
		boolean repeat = iTemplateService.isRepeat(template);
		if(repeat){
			return R.FAILURE("已存在相同模板", null);
		}
		template.setDeadline(DateUtils.getTomorrowEndOfDayDate());
		boolean save = iTemplateService.save(template);
		if(save) {
			return R.OK("保存成功");
		}
		return R.FAILURE("保存失败", null);
		
	}
	
	@GetMapping(value = "getMyTemplates",produces = "application/json")
	public String getMyTemplates() throws JsonProcessingException {
		User sessionUser = getSessionUser("user");
		QueryWrapper<Template>  qu = new QueryWrapper<>();
		qu.eq("userId", sessionUser.getId());
		List<Template> listMaps = iTemplateService.list(qu);
		return R.OK(listMaps);
	}
	@PostMapping(value = "updateTemplate",produces = "application/json")
	public String updateTemplate(@RequestBody Template entity) throws JsonProcessingException {
		User sessionUser = getSessionUser("user");
		Template itemplate = iTemplateService.getById(entity.getId());
		if(sessionUser.getId().intValue() != itemplate.getUserId().intValue()){
			return R.FAILURE("保存失败", null);
		}
		boolean updateById = iTemplateService.updateById(entity);
		if(updateById) {
			return R.OK("保存成功");
		}
		return R.FAILURE("保存失败", null);
	}
	
	@DeleteMapping(value="deleteTemplate/{id}",produces = "application/json")
	public String deleteTemplate(@PathVariable(required = true) Integer id) throws JsonProcessingException {
		User sessionUser = getSessionUser("user");
		Template itemplate = iTemplateService.getById(id);
		if(sessionUser.getId().intValue() != itemplate.getUserId().intValue()){
			return R.FAILURE("删除失败", null);
		}
		boolean removeById = iTemplateService.removeById(id);
		if(removeById) {
			return R.OK("删除成功");
		}
		return R.FAILURE("删除失败", null);
	}
}
